package com.example.dispatherview;

import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

/**
 * Created by Alex-Zhang on 2018/1/8.
 */

public class ExpandView extends LinearLayout {

    private View topView;
    private float foldViewHeight;
    private int default_top_view_height;

    public ExpandView(Context context) {
        super(context);
        init();
    }

    public ExpandView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ExpandView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOrientation(VERTICAL);
        foldViewHeight = dp2px(getContext(), 100);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (getChildCount() < 2) {
            throw new RuntimeException("The number of subviews must be greater than 2");
        }
        topView = getChildAt(0);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        default_top_view_height = topView.getMeasuredHeight();
    }

    private float lastX;
    private float lastY;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        float x = ev.getX();
        float y = ev.getY();

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN: // 按下的时候不分发事件
                lastX = x;
                lastY = y;
                if (y < topView.getMeasuredHeight()){
                    return false;
                }else {
                    return super.dispatchTouchEvent(ev);
                }
            case MotionEvent.ACTION_MOVE:
                float dx = x - lastX;
                float dy = y - lastY;
                lastX = x;
                lastY = y;

                if (Math.abs(dy) < 10 && Math.abs(dx) < 10) {
                    return super.dispatchTouchEvent(ev);
                } else {
                    return handlerView(dy) || super.dispatchTouchEvent(ev);
                }
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                int measuredHeight = topView.getMeasuredHeight();

                if (foldViewHeight < measuredHeight && measuredHeight < (foldViewHeight + (default_top_view_height - foldViewHeight) / 2)) {
                    ViewGroup.LayoutParams layoutParams = topView.getLayoutParams();
                    layoutParams.height = (int) foldViewHeight;
                    topView.setLayoutParams(layoutParams);
                }
                if (default_top_view_height > measuredHeight && measuredHeight >= (foldViewHeight + (default_top_view_height - foldViewHeight) / 2)) {
                    ViewGroup.LayoutParams layoutParams = topView.getLayoutParams();
                    layoutParams.height = default_top_view_height;
                    topView.setLayoutParams(layoutParams);
                }
                return super.dispatchTouchEvent(ev);
            default:
                return super.dispatchTouchEvent(ev);
        }
    }


    private boolean handlerView(float dy) {
        ViewGroup.LayoutParams lp = topView.getLayoutParams();
        if (dy < 0 && topView.getMeasuredHeight() > foldViewHeight) {
            lp.height = (int) (lp.height + dy < foldViewHeight ? foldViewHeight : (lp.height + dy));
            topView.setLayoutParams(lp);
            return true;
        }

        if (dy > 0 && topView.getMeasuredHeight() < default_top_view_height) {
            lp.height = (int) (lp.height + dy < foldViewHeight ? foldViewHeight : (lp.height + dy));
            topView.setLayoutParams(lp);
            return true;
        }
        return false;
    }


    public static int dp2px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }


    @Override
    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        return super.drawChild(canvas, child, drawingTime);

    }

}
